using Microsoft.SqlServer.Management.Sdk.Sfc;
using Microsoft.SqlServer.Management.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class GenScriptSelectForm : WizardPage
	{
		private Label idDescription;

		private Button idBtnSelectAll;

		private Button idBtnClearAll;

		private ListView objectList;

		private ItemCheckedEventHandler clickHandler;

		private GenScriptWizForm parent;

		private GenWizObjectType wizObjectType;

		private string lastDatabaseName = string.Empty;

		public GenScriptSelectForm(WizardForm wizard, GenWizObjectType objType) : base(wizard)
		{
			this.InitializeComponent();
			this.clickHandler = new ItemCheckedEventHandler(this.objectList_ItemChecked);
			this.LoadStrings();
			this.wizObjectType = objType;
			this.parent = (GenScriptWizForm)wizard;
			base.PageID = (int)objType.PageId;
			this.SubTitle = objType.PageSubTitle;
			this.Title = objType.PageTitle;
			base.HelpTopicKeyword = objType.HelpTopicKeyword;
			this.idDescription.Text = objType.PageDescription;
			this.idDescription.AccessibleName = objType.PageTitle;
			int num = 0;
			if (this.wizObjectType.SchemaBased)
			{
				num = 125;
				this.objectList.Columns.Add(GenScriptWizSR.GenScriptSelectSchema, num);
			}
			this.objectList.Columns.Add(GenScriptWizSR.GenScriptSelectName, this.objectList.Width - num);
		}

		protected override void OnInitializePage(EventArgs e)
		{
			this.Wizard.BackEnabled = true;
		}

		protected override void OnEnterPage(EventArgs e)
		{
			GenWizGeneralData genWizGeneralData = (GenWizGeneralData)base.WizardInputs.FindItem(0);
			string databaseName = genWizGeneralData.DatabaseName;
			if (this.lastDatabaseName != databaseName)
			{
				this.lastDatabaseName = databaseName;
				this.OnInitializePage(e);
				this.objectList.ItemChecked -= this.clickHandler;
				this.FillList(genWizGeneralData);
				this.objectList.ItemChecked += this.clickHandler;
			}
			this.CheckBtnState();
		}

		private void LoadStrings()
		{
			this.idBtnSelectAll.Text = GenScriptWizSR.GenScriptSelectFormSelBtnText;
			this.idBtnClearAll.Text = GenScriptWizSR.GenScriptSelectFormClearBtnText;
		}

		private void InitializeComponent()
		{
			this.idDescription = new Label();
			this.idBtnSelectAll = new Button();
			this.idBtnClearAll = new Button();
			this.objectList = new ListView();
			base.SuspendLayout();
			this.idDescription.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.idDescription.Location = new Point(16, 12);
			this.idDescription.Name = "idDescription";
			this.idDescription.Size = new Size(432, 16);
			this.idDescription.TabIndex = 0;
			this.idDescription.Text = GenScriptWizSR.GenScriptSelectFormSelBtnText;
			this.idBtnSelectAll.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.idBtnSelectAll.Enabled = false;
			this.idBtnSelectAll.Location = new Point(16, 210);
			this.idBtnSelectAll.Name = "idBtnSelectAll";
			this.idBtnSelectAll.Size = new Size(108, 23);
			this.idBtnSelectAll.TabIndex = 2;
			this.idBtnSelectAll.Text = GenScriptWizSR.GenScriptSelectFormSelBtnText;
			this.idBtnSelectAll.Click += new EventHandler(this.idSelectAll_Click);
			this.idBtnClearAll.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left);
			this.idBtnClearAll.Enabled = false;
			this.idBtnClearAll.Location = new Point(130, 210);
			this.idBtnClearAll.Name = "idBtnClearAll";
			this.idBtnClearAll.Size = new Size(108, 23);
			this.idBtnClearAll.TabIndex = 3;
			this.idBtnClearAll.Text = GenScriptWizSR.GenScriptSelectFormClearBtnText;
			this.idBtnClearAll.Click += new EventHandler(this.idClearAll_Click);
			this.objectList.Activation = ItemActivation.OneClick;
			this.objectList.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.objectList.CheckBoxes = true;
			this.objectList.FullRowSelect = true;
			this.objectList.HeaderStyle = ColumnHeaderStyle.Nonclickable;
			this.objectList.Location = new Point(16, 36);
			this.objectList.Name = "objectList";
			this.objectList.ShowGroups = false;
			this.objectList.Size = new Size(432, 160);
			this.objectList.TabIndex = 4;
			this.objectList.View = View.Details;
			this.objectList.Resize += new EventHandler(this.objectList_Resize);
			base.Controls.Add(this.objectList);
			base.Controls.Add(this.idBtnClearAll);
			base.Controls.Add(this.idBtnSelectAll);
			base.Controls.Add(this.idDescription);
			base.Name = "GenScriptSelectForm";
			base.Size = new Size(464, 245);
			base.ResumeLayout(false);
		}

		private void CheckBtnState()
		{
			int count = this.objectList.CheckedItems.Count;
			this.idBtnSelectAll.Enabled = (count != this.objectList.Items.Count);
			this.idBtnClearAll.Enabled = (count != 0);
			this.Wizard.NextEnabled = (count != 0);
			base.FinishEnabled = this.parent.CanEnableFinish();
		}

		private void idSelectAll_Click(object sender, EventArgs e)
		{
			this.objectList.ItemChecked -= this.clickHandler;
			this.wizObjectType.SelectedUrns.Clear();
			foreach (ListViewItem listViewItem in this.objectList.Items)
			{
				listViewItem.Checked = true;
				this.wizObjectType.SelectedUrns.Add((Urn)listViewItem.Tag);
			}
			this.objectList.ItemChecked += this.clickHandler;
			this.CheckBtnState();
		}

		private void idClearAll_Click(object sender, EventArgs e)
		{
			this.objectList.ItemChecked -= this.clickHandler;
			foreach (ListViewItem listViewItem in this.objectList.Items)
			{
				listViewItem.Checked = false;
			}
			this.wizObjectType.SelectedUrns.Clear();
			this.objectList.ItemChecked += this.clickHandler;
			this.CheckBtnState();
		}

		private void FillList(GenWizGeneralData genData)
		{
			this.objectList.Items.Clear();
			bool flag;
			List<ObjectInfo> objectsList = genData.GetObjectsList(this.wizObjectType.PageId, false, out flag);
			foreach (ObjectInfo current in objectsList)
			{
				ListViewItem listViewItem;
				if (this.wizObjectType.SchemaBased)
				{
					listViewItem = new ListViewItem(new string[]
					{
						current.Schema,
						current.Name
					});
				}
				else
				{
					listViewItem = new ListViewItem(current.Name);
				}
				listViewItem.Tag = current.Urn;
				this.objectList.Items.Add(listViewItem);
			}
		}

		private void objectList_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			if (e.Item.Checked)
			{
				this.wizObjectType.SelectedUrns.Add((Urn)e.Item.Tag);
			}
			else
			{
				this.wizObjectType.SelectedUrns.Remove((Urn)e.Item.Tag);
			}
			this.CheckBtnState();
		}

		private void objectList_Resize(object sender, EventArgs e)
		{
			int num = this.objectList.Columns.Count - 1;
			int width;
			if (num == 1)
			{
				width = this.objectList.Width - this.objectList.Columns[0].Width;
			}
			else
			{
				width = this.objectList.Width;
			}
			this.objectList.Columns[num].Width = width;
		}
	}
}
